

import org.apache.spark.SparkContext
import org.apache.spark.sql.{DataFrame, SparkSession}

import java.util.Properties

object SparkSqlMysqlDemo01 {
  def main(args: Array[String]): Unit = {
    //创建SparkSession对象
    val spark = SparkSession.builder().appName("sparksql-demo").master("local[2]").getOrCreate()
    //rdd--> dataframe
    //创建SparkContext: 由SparkSession对象得到SparkContext对象
    val sc: SparkContext = spark.sparkContext
    sc.setLogLevel("WARN")

    //读取mysql数据库中的数据，封装到dataframe
    val url="jdbc:mysql://10.104.13.42:3306/student?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false"
    val prop = new Properties()
    prop.setProperty("user","root")
    prop.setProperty("password","123456")
    val personDf: DataFrame = spark.read.jdbc(url, "emp", prop)

    //写入
//    personDf.write.mode("append").jdbc(url,"person1",prop)
personDf.show();



  }

}
